<template>
  <div class="divBox">
    <el-card class="box-card">
      <div slot="header" class="clearfix">
        <div class="container">
          <el-form inline>
            <el-form-item label="是否显示：">
              <el-select v-model="tableFrom.isShow" placeholder="请选择" class="filter-item selWidth" @change="getList(1)" clearable>
                <el-option label="关闭" :value="false" />
                <el-option label="开启" :value="true" />
              </el-select>
            </el-form-item>
            <el-form-item label="购买方式：">
              <el-select v-model="tableFrom.type" placeholder="请选择" class="filter-item selWidth" @change="getList(1)" clearable>
                <el-option label="积分购买" :value="0" />
                <el-option label="积分+金额购买" :value="1" />
              </el-select>
            </el-form-item>
            <el-form-item label="商品分类：">
              <el-select v-model="tableFrom.cateId" placeholder="请选择"   class="filter-item selWidth" @change="getList(1)" clearable>
                <el-option
                  v-for="item in treeList"
                  :key="item.id"
                  :label="item.name"
                  :value="item.id"
                />
              </el-select>
            </el-form-item>
            <el-form-item label="商品搜索：">
              <el-input v-model="tableFrom.productName" placeholder="请输入商品名称" class="selWidth" clearable>
                <el-button slot="append" icon="el-icon-search" @click="getList(1)"/>
              </el-input>
            </el-form-item>
          </el-form>
        </div>
        <router-link :to=" { path:'/course/integralBuy/creatIntegralBuy/create' }">
          <el-button size="small" type="primary" class="mr10">添加积分商品</el-button>
        </router-link>
      </div>
      <el-table
        v-loading="listLoading"
        :data="tableData.data"
        style="width: 100%"
        size="mini"
        ref="multipleTable"
      >
        <el-table-column label="商品图片" min-width="80">
          <template slot-scope="scope">
            <div class="demo-image__preview">
              <el-image
                style="width: 36px; height: 36px"
                :src="scope.row.image"
                :preview-src-list="[scope.row.image]"
              />
            </div>
          </template>
        </el-table-column>
        <el-table-column
          label="活动简介"
          min-width="180"
          prop="info"
        />
        <el-table-column
          label="原价"
          prop="otPrice"
          min-width="100"
        />
        <el-table-column
          label="积分价"
          min-width="100"
          prop="integralPrice"
        >
          <template slot-scope="scope">
            {{scope.row.integralPrice}}{{scope.row.price-0>0?'+￥'+scope.row.price:''}}
          </template>
        </el-table-column>
        <el-table-column
          label="销量"
          prop="sales"
          min-width="80"
        />
        <el-table-column
          label="库存"
          min-width="80"
          prop="stock"
        />
        <el-table-column
          label="商品分类"
          min-width="100"
          prop="cateStr"
        />
        <el-table-column
          label="积分状态"
          min-width="100"
          prop="statusName"
        />
        <el-table-column
          label="状态"
          min-width="150"
        >
          <template slot-scope="scope">
            <el-switch
              v-model="scope.row.isShow"
              :active-value="true"
              :inactive-value="false"
              active-text="开启"
              inactive-text="关闭"
              @change="onchangeIsShow(scope.row)"
            />
          </template>
        </el-table-column>
        <el-table-column
          label="创建时间"
          prop="createTime"
          min-width="150"
        />
        <el-table-column label="操作" min-width="150" fixed="right" align="center">
          <template slot-scope="scope">
            <router-link :to="{ path:'/course/integralBuy/creatIntegralBuy/updeta/' + scope.row.productId + '/' + scope.row.id}">
              <el-button type="text" size="small">编辑</el-button>
            </router-link>
            <el-button v-if="scope.row.killStatus !== 2 " type="text" size="small" @click="handleDelete(scope.row.id, scope.$index)"  class="mr10">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
      <div class="block mb20">
        <el-pagination
          :page-sizes="[10, 20, 30, 40]"
          :page-size="tableFrom.limit"
          :current-page="tableFrom.page"
          layout="total, sizes, prev, pager, next, jumper"
          :total="tableData.total"
          @size-change="handleSizeChange"
          @current-change="pageChange"
        />
      </div>
    </el-card>
  </div>
</template>

<script>
  import { integralGoodsListApi, integralGoodsDeleteApi, integralGoodsUpdateApi, integralGoodsStatusApi } from '@/api/marketing'
  import * as categoryApi2 from "@/api/categoryApi";
  export default {
    name: "SeckillList",
    data() {
      return {
        listLoading: true,
        tableData: {
          data: [],
          total: 0
        },
        tableFrom: {
          page: 1,
          limit: 20,
          type: '',
          cateId: '',
          isShow: true,
          productName: ''
        },
        treeList:[]
      }
    },
    mounted() {
      this.getTree();
    },
    methods: {
      getTree(){
        categoryApi2.treeCategroy({ type: 10, status: -1, name: ''}).then(res => {
          console.log('res=>',res)
          // if(res.code==200){
          this.treeList = res
          this.getList();
          // console.log('tree')
          // console.log(this.treeList)
          // }
        })
      },
      // 订单删除
      handleDelete(id, idx) {
        this.$modalSure().then(() => {
          console.info(id)
          integralGoodsDeleteApi({ id: id }).then(() => {
            this.$message.success('删除成功')
            this.tableData.data.splice(idx, 1)
          })
        })
      },
      // 列表
      getList(num) {
        this.listLoading = true
        this.tableFrom.page = num ? num : this.tableFrom.page;
        integralGoodsListApi(this.tableFrom).then(res => {
          for(let i in res.list){
            let t = '未知'
            for(let l in this.treeList){
              if(res.list[i].cateId==this.treeList[l].id){
                t=this.treeList[l].name
              }
            }
            res.list[i].cateStr=t
          }
          this.tableData.data = res.list
          this.tableData.total = res.total
          this.listLoading = false
        }).catch((res) => {
          this.listLoading = false
        })
      },
      pageChange(page) {
        this.tableFrom.page = page
        this.getList()
      },
      handleSizeChange(val) {
        this.tableFrom.limit = val
        this.getList()
      },
      onchangeIsShow(row) {
        integralGoodsStatusApi({id: row.id, isShow: row.isShow})
          .then(async () => {
            this.$message.success('修改成功');
            this.getList()
          }).catch(()=>{
          row.status = !row.status
        })
      }
    }
  }
</script>

<style scoped>

</style>
